package com.chj.design.sk;

public class Code10_Memento {

	public static class Role {
		private int bloodFlow;
		private int magicPoint;

		public Role(int bloodFlow, int magicPoint) {
			this.bloodFlow = bloodFlow;
			this.magicPoint = magicPoint;
		}

		public int getBloodFlow() {
			return bloodFlow;
		}

		public void setBloodFlow(int bloodFlow) {
			this.bloodFlow = bloodFlow;
		}

		public int getMagicPoint() {
			return magicPoint;
		}

		public void setMagicPoint(int magicPoint) {
			this.magicPoint = magicPoint;
		}

		/**
		 * @desc 展示角色当前状态
		 * @return void
		 */
		public void display() {
			System.out.println("用户当前状态:");
			System.out.println("血量:" + getBloodFlow() + ";蓝量:" + getMagicPoint());
		}

		/**
		 * @desc 保持存档、当前状态
		 * @return
		 * @return Memento
		 */
		public Memento saveMemento() {
			return new Memento(getBloodFlow(), getMagicPoint());
		}

		/**
		 * @desc 恢复存档
		 * @param memento
		 * @return void
		 */
		public void restoreMemento(Memento memento) {
			this.bloodFlow = memento.getBloodFlow();
			this.magicPoint = memento.getMagicPoint();
		}
	}

	public static class Memento {
		private int bloodFlow;
		private int magicPoint;

		public int getBloodFlow() {
			return bloodFlow;
		}

		public void setBloodFlow(int bloodFlow) {
			this.bloodFlow = bloodFlow;
		}

		public int getMagicPoint() {
			return magicPoint;
		}

		public void setMagicPoint(int magicPoint) {
			this.magicPoint = magicPoint;
		}

		public Memento(int bloodFlow, int magicPoint) {
			this.bloodFlow = bloodFlow;
			this.magicPoint = magicPoint;
		}
	}

	public static class Caretaker {
		Memento memento;

		public Memento getMemento() {
			return memento;
		}

		public void setMemento(Memento memento) {
			this.memento = memento;
		}
	}

	public static void main(String[] args) {
		// 打BOSS之前：血、蓝全部满值
		Role role = new Role(100, 100);
		System.out.println("----------大战BOSS之前----------");
		role.display();

		// 保持进度
		Caretaker caretaker = new Caretaker();
		caretaker.memento = role.saveMemento();

		// 大战BOSS，快come Over了
		role.setBloodFlow(20);
		role.setMagicPoint(20);
		System.out.println("----------大战BOSS----------");
		role.display();

		// 恢复存档
		role.restoreMemento(caretaker.getMemento());
		System.out.println("----------恢复----------");
		role.display();

	}
}
